import ScreenShot  from "js-web-screen-shot";
import { getPrintScr } from '@/utils/Electron'
type CallbackFunction = (param: any) => void;

// 发送消息
export const getDesktopCapturerSource = async () => {
    return await (window as any).ipcRenderer.invoke("ev:send-desktop-capturer_source", []);
};


// 获取指定id设备的视频流
export const getInitStream = async (
    source: { id: string },
    audio?: boolean
): Promise<MediaStream | null> =>{
    return new Promise((resolve, _reject) => {
    // 获取指定窗口的媒体流
    // 此处遵循的是webRTC的接口类型  暂时TS类型没有支持  只能断言成any
    (navigator.mediaDevices as any)
        .getUserMedia({
        audio: audio
            ? {
                mandatory: {
                    chromeMediaSource: "desktop",
                },
            }
            : false,
        video: {
            mandatory: {
                chromeMediaSource: "desktop",
                chromeMediaSourceId: source.id,
            },
        },
        })
        .then((stream: MediaStream) => {
            resolve(stream);
        })
        .catch((error: any) => {
        console.log(error);
            resolve(null);
        });
    });
}

export default async(callback: CallbackFunction) => {
    console.log("开始截屏")
    // 获取屏幕流
    // getWindowsInfo().then((res: any)=> {
    //     getInitStream(res[0]).then(res1 => {
    //         new ScreenShot ({
    //             enableWebRtc: true,  
    //             screenFlow: res1!, // 传入屏幕流数据
    //             clickCutFullScreen: true,
    //             level: 9999,  //层级级别
    //             completeCallback: callback
    //         })
    //     })
    // })

    // Canvas截图
    // new ScreenShot ({
    //     enableWebRtc: true,  
    //     clickCutFullScreen: true,
    //     level: 9999,  //层级级别
    //     completeCallback: callback
    // })

    // QQ截图
    getPrintScr?.(callback)
}